﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Project
{
    public partial class Frm_Project : BasicMdiChildForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_Project()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.gv_SubProjects.CustomDrawCell += (s, e) =>
            {
                this.gv_SubProjects.CustomDrawCell<SubProject>(e,
                    (subPrjt) =>
                    {
                        return !subPrjt.Effective;
                    },
                    () =>
                    {
                        e.Appearance.ForeColor = Color.Red;
                    });
            };

            this.gv_Projects.CustomDrawCell += (s, e) =>
            {
                this.gv_Projects.CustomDrawCell<Contract.Project>(e,
                    (prjt) =>
                    {
                        return !prjt.Effective;
                    },
                    () =>
                    {
                        e.Appearance.ForeColor = Color.Red;
                    });
            };

            this.gv_Projects.FocusedRowChanged += (s, e) =>
                {
                    SelPrjt = this.gv_Projects.GetRow(e.FocusedRowHandle) as Contract.Project;
                };
        }

        protected override void LoadDatas()
        {
            this.bds_Project.BindingData(_masterProxy.GetAllProject().ReturnValue);
            this.cmb_ReportStyle.Properties.BindingData(ReportStyle.GetDatas());        
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Edit":
                    this.edit();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void add()
        {
            Frm_ProjectEdit frm = new Frm_ProjectEdit();
            frm.Text = "增加项目";
            frm.Load += (s, e) =>
                {
                    Contract.Project project = new Contract.Project();
                    project.Editable = true;
                    frm.CurPrjt = project;
                };
            updateDatas(FormOpener.ShowDialog(frm));
        }

        private void edit()
        {
            if (!SelPrjt.Editable)
            {
                MessageBox.Show("这项不能编辑");
                return;
            }
            Frm_ProjectEdit frm = new Frm_ProjectEdit();
            frm.Text = "修改项目资料";
            frm.Load += (s, e) =>
                {
                    frm.CurPrjt = SelPrjt;
                };
            updateDatas(FormOpener.ShowDialog(frm));
        }

        private void updateDatas(DialogResult dialogResult)
        {
            if (dialogResult == DialogResult.OK)
                LoadDatas();
        }

        private Contract.Project _selPrjt;
        public Contract.Project SelPrjt
        {
            get { return _selPrjt; }
            set
            {
                _selPrjt = value;
                onSelPrjtChanged();
            }
        }

        private void onSelPrjtChanged()
        {
            if (SelPrjt == null)
            {
                // clear project datas
                ControlExtensions.ClearValue(
                    this.txt_Name,
                    this.cmb_ReportStyle
                    );

                this.bds_SubProject.BindingData(null);
            }
            else
            {
                // set project datas

                this.txt_Name.SetText(SelPrjt.Name);
                this.cmb_ReportStyle.SetValue(SelPrjt.ReportStyle, "Value");

                this.bds_SubProject.BindingData(SelPrjt.SubProjectList);
            }
        }
    }
}
